package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;
import java.util.Random;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.StrategyAdapter;
import flc.ast.databinding.ActivityStrategyBinding;
import stark.common.api.StkApi;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

public class StrategyActivity extends BaseAc<ActivityStrategyBinding> {

    public static int type;
    private StrategyAdapter strategyAdapter;
    private int page = 1;
    private int refreshTime = 200;

    @Override
    protected int onCreate() {
        return R.layout.activity_strategy;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event5Container);
        mDataBinding.ivStrategyBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.rvStrategyList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        strategyAdapter = new StrategyAdapter();
        mDataBinding.rvStrategyList.setAdapter(strategyAdapter);
        strategyAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {
        if (type == 0) {
            mDataBinding.tvGameName.setText(getString(R.string.game_name1));
            mDataBinding.tvGameDesc.setText(getString(R.string.game_desc1));
            mDataBinding.ivStrategyImg.setImageResource(R.drawable.adanzaipaid2);
            mDataBinding.ivStrategyBackground.setImageResource(R.drawable.adanzaipaid);
            mDataBinding.ivStrategyAvatar.setImageResource(R.drawable.atoux05);
            getData(AppConfig.STRATEGY_DATA1);
        } else if (type == 1) {
            mDataBinding.tvGameName.setText(getString(R.string.game_name2));
            mDataBinding.tvGameDesc.setText(getString(R.string.game_desc2));
            mDataBinding.ivStrategyImg.setImageResource(R.drawable.adafuw2);
            mDataBinding.ivStrategyBackground.setImageResource(R.drawable.adafuwbj);
            mDataBinding.ivStrategyAvatar.setImageResource(R.drawable.atouxiang03);
            getData(AppConfig.STRATEGY_DATA2);
        } else if (type == 2) {
            mDataBinding.tvGameName.setText(getString(R.string.game_name3));
            mDataBinding.tvGameDesc.setText(getString(R.string.game_desc3));
            mDataBinding.ivStrategyImg.setImageResource(R.drawable.ayundzzy);
            mDataBinding.ivStrategyBackground.setImageResource(R.drawable.ayundinzybj);
            mDataBinding.ivStrategyAvatar.setImageResource(R.drawable.atoux02);
            getData(AppConfig.STRATEGY_DATA3);
        } else if (type == 3) {
            mDataBinding.tvGameName.setText(getString(R.string.game_name4));
            mDataBinding.tvGameDesc.setText(getString(R.string.game_desc4));
            mDataBinding.ivStrategyImg.setImageResource(R.drawable.ahunaochuf);
            mDataBinding.ivStrategyBackground.setImageResource(R.drawable.ahunaochufangbj);
            mDataBinding.ivStrategyAvatar.setImageResource(R.drawable.atoux02);
            getData(AppConfig.STRATEGY_DATA4);
        }
        Random random=new Random();
        int num=random.nextInt(2000)+1000;
        mDataBinding.tvStrategyReadNum.setText(getString(R.string.strategy_read_num,num));
        mDataBinding.refreshStrategyItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshStrategyItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshStrategyItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                if (type == 0) {
                    getData(AppConfig.STRATEGY_DATA1);
                }else if (type == 1) {
                    getData(AppConfig.STRATEGY_DATA2);
                }else if (type == 2) {
                    getData(AppConfig.STRATEGY_DATA3);
                }else if (type == 3) {
                    getData(AppConfig.STRATEGY_DATA4);
                }
                mDataBinding.refreshStrategyItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                if (type == 0) {
                    getData(AppConfig.STRATEGY_DATA1);
                }else if (type == 1) {
                    getData(AppConfig.STRATEGY_DATA2);
                }else if (type == 2) {
                    getData(AppConfig.STRATEGY_DATA3);
                }else if (type == 3) {
                    getData(AppConfig.STRATEGY_DATA4);
                }
                mDataBinding.refreshStrategyItem.finishRefresh(refreshTime);
            }
        });
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean=strategyAdapter.getItem(position);
        BaseWebviewActivity.open(mContext,bean.getUrl(),bean.getName());
    }

    private void getData(String url) {
        StkResApi.getTagResourceList(null, url, StkApi.createParamMap(0, 10),false,
                new IReqRetCallback<List<StkResBean>>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                        if (success) {
                            if (result != null && result.size() > 0) {
                                if (page == 1) {
                                    strategyAdapter.setList(result);
                                } else {
                                    strategyAdapter.addData(result);
                                }
                            }
                        }
                    }
                });
    }


}